define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/admin/index',
                    add_url: 'auth/admin/add',
                    edit_url: 'auth/admin/edit',
                    del_url: 'auth/admin/del',
                    multi_url: 'auth/admin/multi',
                }
            });

            var table = $("#table");

            //在表格内容渲染完成后回调的事件
            table.on('post-body.bs.table', function (e, json) {
                $("tbody tr[data-index]", this).each(function () {
                    if (parseInt($("td:eq(1)", this).text()) == Config.admin.id) {
                        $("input[type=checkbox]", this).prop("disabled", true);
                    }
                });
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                showExport: true,
                exportTypes: ['excel'],
                showColumns: false,
                clickToSelect: false,
                dblClickToEdit: false,
                singleSelect: false,
                pageSize:20,
                pageList: [20, 50],
                columns: [
                    [
                        {field: 'state', checkbox: true, },
                        {field: 'id', title: 'ID'},
                        {field: 'username', title: __('用户名称')},
                        {field: 'company_name', title: __('公司名称')},
                        {field: 'telephone', title: __('联系电话')},
                        //{field: 'nickname', title: __('Nickname')},
                        {field: 'groups_text', title: __('Group')},
                        {field: 'line', title: __('外呼线路'), operate:false,
                        formatter: function (value, row, index) {
                            return ' <div style="width:200px;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;cursor:pointer;" title="' + value + '">' +value + '</div>';
                        }},
                        {field: 'auth_number', title: __('线路数量'), operate:false},
			// {field: 'admin.nickname', title: __('所属门店组长'), operate:false, formatter: Table.api.formatter.label},
			//{field: 'extension', title: '分机号码'},
			//{field: 'extension_password', title: '分机密码'},
                        /*{field: 'email', title: __('Email')},*/
                        // {field: 'status', title: __("Status"), formatter: Table.api.formatter.status},
                        // {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'createtime',
                            title: __('创建时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
                                if(row.id == Config.admin.id){
                                    return '';
                                }
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }}
                    ]
                ]
            });
            if($('.status').text()=='1'){
                $('.main-status').show();
            }
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $.get("/admin/auth/admin/company", function (data) {
                    $('.line-num').text(data[0].auth_number);
                    $('#auth_number').attr('max',data[0].auth_number);
            });
            $(document).on("click", ".inner li", function () {
                keyupNum();
            });  
            $(".checked-l input").click(function () {
                var val = $('.checked-l input:checked').val();
                if (val != null) {
                    $('.checked-l input,.auth_num input').val('1');
                    $('.auth_num_tex').show();
                    if($('.filter-option.pull-left').text().indexOf("个人账号") !='-1'){
                        $('.auth_num_tex').hide();
                    }
                } else {
                    $('.checked-l input,.auth_num input').val('');
                    $('.auth_num_tex').hide();
                }
            });
            keyupNum();
            function keyupNum(){
                var textdiv=$('.filter-option.pull-left').text();
                if($('.status').text()=='1'){
                    if(textdiv.indexOf("公司账号") !='-1' || textdiv.indexOf("个人账号") !='-1' ||textdiv.indexOf("一级代理") !='-1' ||textdiv.indexOf("二级代理") !='-1'){     
                        $('.auth_t,.auth_num_tex').show();
                        $('.checked-l input').prop('checked',true);
                    }else{
                        $('.auth_t,.auth_num_tex').hide();
                        $('.checked-l input').prop('checked',false);
                    }
                }
            }
            Form.api.bindevent($("form[role=form]"));
            /*$('#username').change(function() {
                $('#extension').val($(this).val());
            });
            $('#password').change(function() {
                $('#extension_password').val($(this).val());
            });*/
        },
        edit: function () {
            $.get("/admin/auth/admin/company", function (data) {
                $('.line-num').text(data[0].auth_number);
                $('#auth_number').attr('max',data[0].auth_number);
            });
            $(".checked-l input").click(function () {
                var val = $('.checked-l input:checked').val();
                if (val != null) {
                    $('.checked-l input').val('1');
                    $('.auth_num_tex').show();
                } else {
                    $('.checked-l input').val('');
                    $('.auth_num_tex').hide();
                }
            });
            if($('.status').text()!='1'){
                $('#username').css({'pointer-events': 'none'});
                $('.auth_group').css({'pointer-events': 'none'});
            }
            if ($('.checked-l input:checked').val() != null) {
                $('.auth_num_tex').show();
            } 
            if($('.checked-l input').val() !='0'){
                $('.auth_num_tex,.auth_t').show();
                $('.checked-l input').css({'pointer-events': 'none'});
                $('.checked-l input').prop('checked',true);
            }else{
                $('.auth_num_tex,.auth_t').hide();
                $('.checked-l input').css({'pointer-events': 'auto'});
                $('.checked-l input').prop('checked',false);
            }
            var company_name=$('#company_name').val();
            var telephone=$('#telephone').val();
            var auth_number=$('#auth_number').val();
            $(document).on("click", ".inner li", function () {
                var textdiv=$('.filter-option.pull-left').text();
                if($('.status').text()=='1'){
                    if(textdiv.indexOf("公司账号") !='-1' || textdiv.indexOf("个人账号") !='-1' ||textdiv.indexOf("一级代理") !='-1' ||textdiv.indexOf("二级代理") !='-1'){     
                        if(textdiv.indexOf("一级代理") !='-1'){     
                            $('.levl').val('1');
                        }else if(textdiv.indexOf("二级代理") !='-1'){
                            $('.levl').val('2');
                        }else if(textdiv.indexOf("公司账号") !='-1'){
                            $('.levl').val('3');
                        }else if(textdiv.indexOf("个人账号") !='-1'){
                            $('.levl').val('4');
                        }
                        $('#company_name').val(company_name);
                        $('#telephone').val(telephone);
                        $('#auth_number').val(auth_number);
                        $('.auth_num_tex,.auth_t').show();
                        $('.checked-l input').css({'pointer-events': 'auto'});
                        $('.checked-l input').prop('checked',true);
                    }else{
                        $('.levl').val('0');
                        $('#company_name').val("");
                        $('#telephone').val("");
                        $('#auth_number').val("");
                        $('.auth_num_tex,.auth_t').hide();
                        $('.checked-l input').css({'pointer-events': 'none'});
                        $('.checked-l input').prop('checked',false);
                    }
                }
            });  
            Form.api.bindevent($("form[role=form]"));
            /*$('#username').change(function() {
                $('#extension').val($(this).val());
            });
            $('#password').change(function() {
                $('#extension_password').val($(this).val());
            });*/ 
        }
    };
    return Controller;
});